<template>
  <div class="container">
        <!-- 搜索框 -->
    <div class="search-box">
      <el-input
        v-model="queryParams.storeName"
        placeholder="请输入你要搜索的实体店"
        class="search-input"
        clearable
      >
      </el-input>
      <el-button type="primary" @click="handleSearch">搜索</el-button>
    </div>

    <!-- 实体店筛选条件 -->
    <div class="filter-section">
      <span class="filter-label">类型1</span>
      <el-select v-model="selectedFilters.type1" placeholder="请选择">
        <el-option label="条件1" value="1" />
        <el-option label="条件2" value="2" />
      </el-select>
      
      <span class="filter-label" style="margin-left: 20px">类型1：</span>
      <el-select v-model="selectedFilters.type2" placeholder="请选择">
        <el-option label="条件1" value="1" />
        <el-option label="条件2" value="2" />
        <el-option label="条件3" value="3" />
      </el-select>
    </div>

    <!-- 实体店列表 -->
      <el-row :gutter="20">
        <el-empty v-if="!storeList" description="暂无店铺数据" />
        <el-col :span="8" v-for="item in pageList" :key="item.storeId">
          <el-card class="store-card" shadow="hover" @click="goToStoreDetail(item.userId)">
            <div class="store-images">
              <el-image 
                v-for="(img, index) in item.images" 
                :key="index"
                :src="img" 
                fit="cover"
              />
            </div>
            <div class="store-info">
              <h3>{{ item.storeName }}</h3>
              <p class="description">{{ item.description }}</p>
              <p class="location">{{ item.location }}</p>
              <div class="sales">
                <span>店铺销量{{ item.sales }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!--分页区域-->
      <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[9]"
            :small="small"
            :disabled="disabled"
            :background="background"
            layout="sizes,prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            style="position: absolute ;bottom: 30px; margin-top: 30px; margin-left: 20%;"
        />
  </div>
</template>

<script setup>
import { ref,onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { searchStoreApi } from '@/api/search'

const queryParams = ref({
  storeName: ''
})
const selectedFilters = ref({
  type1: '',
  type2: ''
})

// 模拟数据
// const storeList = ref([
//   {
//     id: 1,
//     name: '商务休闲衬衫',
//     company: '桃褶服装股份有限公司',
//     location: '贵州省贵阳市花溪区清溪路',
//     rating: 4.8,
//     sales:120,
//     images: [
//       'https://via.placeholder.com/150',
//       'https://via.placeholder.com/150',
//       'https://via.placeholder.com/150'
//     ]
//   },
//     {
//     id: 1,
//     name: '商务休闲衬衫',
//     company: '桃褶服装股份有限公司',
//     location: '贵州省贵阳市花溪区清溪路',
//     rating: 4.8,
//     sales:120,
//     images: [
//       'https://via.placeholder.com/150',
//       'https://via.placeholder.com/150',
//       'https://via.placeholder.com/150'
//     ]
//   },
//     {
//     id: 1,
//     name: '商务休闲衬衫',
//     company: '桃褶服装股份有限公司',
//     location: '贵州省贵阳市花溪区清溪路',
//     rating: 4.8,
//     sales:120,
//     images: [
//       'https://via.placeholder.com/150',
//       'https://via.placeholder.com/150',
//       'https://via.placeholder.com/150'
//     ]
//   },
//     {
//     id: 1,
//     name: '商务休闲衬衫',
//     company: '桃褶服装股份有限公司',
//     location: '贵州省贵阳市花溪区清溪路',
//     rating: 4.8,
//     sales:120,
//     images: [
//       'https://via.placeholder.com/150',
//       'https://via.placeholder.com/150',
//       'https://via.placeholder.com/150'
//     ]
//   },
//   // ... 更多数据
// ])

const storeList = ref([])
const pageList = ref([]); // 当前页的数据（用于显示）
// 实体店数据
const getstoreList = async () => { 
  const res = await searchStoreApi()
  storeList.value = res.data
  total.value = storeList.value.length
  pagePages.value = Math.ceil(total.value / pageSize.value); // 计算总页数
  updatePaginatedList();   // 更新分页数据
}
// 确保在组件挂载时调用
onMounted(() => {
  getstoreList()
})


// 搜索处理
const handleSearch = async () => {
  const res = await searchStoreApi(queryParams.value)
  storeList.value = res.data
  total.value = storeList.value.length
  pagePages.value = Math.ceil(total.value / pageSize.value); // 计算总页数
  updatePaginatedList();   // 更新分页数据
}

//分页
const total = ref(0); // 总条数
const pagePages = ref(1); // 总页数
const currentPage = ref(1); // 当前页码
const pageSize = ref(9);  // 每页显示条数

// 更新分页数据（根据 currentPage 和 pageSize）
const updatePaginatedList = () => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  pageList.value = storeList.value.slice(start, end);  // 切割出当前页的数据
};

// 处理每页条数变化
const handleSizeChange = (newSize) => {
  pageSize.value = newSize; // 更新每页显示的条目数
  updatePaginatedList();  // 更新分页数据
};

// 处理当前页码变化
const handleCurrentChange = (newPage) => {
  currentPage.value = newPage; // 更新当前页码
  updatePaginatedList();  // 更新分页数据
};

// 跳转详情
const router = useRouter()
//跳转实体店详情
const goToStoreDetail = (id) => {
  const routeData = router.resolve({
    name: 'storeDetail',
    params: { userId: id }
  })
  // 在新窗口打开
  window.open(routeData.href, '_blank')
}

</script>

<style scoped lang="scss">
.container{
  min-height: 100vh;
}
.search-box {
  max-width: 600px;
  margin: 0 auto 20px;
  display: flex;
  .search-input {
    width: 100%;
  }
  .el-button {
    margin-left: 20px;
    width: 80px;
  }
}

.filter-section {
  margin: 20px 0;
  display: flex;
  align-items: center;
  .el-select {
    width: 150px;
  }
}

.filter-label {
  margin-right: 10px;
  color: #606266;
}

.filter-tag {
  margin-right: 10px;
  cursor: pointer;
}

.store-card {
  margin-bottom: 20px;
}

.store-images {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.store-images .el-image {
  width: 100px;
  height: 100px;
}

.store-info{
  p{
    color: #606266;
    font-size: 14px;
    margin-bottom: 10px;
  }
  h3{
  margin: 0 0 10px 0;
  }
}

/* 响应式布局 */
@media screen and (max-width: 1200px) {
  .el-col {
    width: 25%;
  }
}

@media screen and (max-width: 992px) {
  .el-col {
    width: 33.33%;
  }
}

@media screen and (max-width: 768px) {
  .el-col {
    width: 50%;
  }
}

@media screen and (max-width: 576px) {
  .el-col {
    width: 100%;
  }
}
</style>